//
//  NYChoseBookViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/9.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYChoseBookViewController.h"
//vc
#import "NYAirlineViewController.h"
#import "NYCourseViewController.h"

//request
#import "NYGradeRequest.h"


@interface NYChoseBookViewController ()

@property (nonatomic, strong) UIImageView *classImageview;
@property (nonatomic, strong) QMUIButton *msgButton;
@property (nonatomic, strong) QMUIButton *gradeButton;
@property (nonatomic, strong) QMUIPopupMenuView *popupView;


@property(nonatomic, strong) NYGradeRequest *request;
@end

@implementation NYChoseBookViewController

#pragma mark - Life Cycle Methods

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initUI];
}
#pragma mark - Intial Methods
- (void)initUI {
    
    [self setupSubControllers];
    
    [self loadData];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (NYUserManager.isLogin && self.isLoadDataError && self.isQmui_dataLoaded) {
        [self loadData];
    }
}
- (void)initSubviews {
    
    [super initSubviews];
}
- (void)handleItemButtonAction {
    
    @weakify(self);
    self.itemController.itemBlock = ^(NYGradeItemListModel *itemModel) {
        
        @strongify(self);
        [NYUserManager pushOrPresentLoginIfNeed:YES inViewController:nil completion:nil dismiss:^{
            if (NYUserManager.isLogin) {
                [self.navigationController pushViewController:[[NYCourseViewController alloc] initWithDetailContentStatus:NYExamineRequestStatusChoose withId:[NSString stringWithFormat:@"%ld",(long)itemModel.id]] animated:true];
            }
        }];
    };
}
- (void)setupSubControllers {
    
    self.gradeController = [NYGradeViewController new];
    self.itemController = [NYGradeItemViewController new];
    
    @weakify(self);
    self.gradeController.selectedBlock = ^(NYGradeModel *gradeModel) {
        
        @strongify(self);
        
        self.itemController.itemMutaArray = [NSMutableArray arrayWithArray:gradeModel.know];
    };
    
    [self handleItemButtonAction];

    [self.gradeController willMoveToParentViewController:self];
    [self.itemController willMoveToParentViewController:self];
    [self.gradeController beginAppearanceTransition:YES animated:YES];
    [self.itemController beginAppearanceTransition:YES animated:YES];

    [self addChildViewController:self.gradeController];
    self.controllerView.gradeView = self.gradeController.view;

    [self addChildViewController:self.itemController];
    self.controllerView.itemView = self.itemController.view;

    [self.itemController endAppearanceTransition];
    [self.gradeController endAppearanceTransition];
    [self.gradeController didMoveToParentViewController:self];
    [self.itemController didMoveToParentViewController:self];
}

#pragma mark - Network Methods
- (void)loadData {
    [self setLoading:YES animation:YES];
    [self.request clearCache];

    self.request = [NYGradeRequest requestWithParams:@{kRequestKeyChannel:@(NYUserManager.channelID), kRequestKeyLoginCode : NOT_NULL_STRING_DEFAULT_EMPTY(NYUserManager.logincode), kRequestKeyUser_ID:NOT_NULL_STRING_DEFAULT_EMPTY(NYUserManager.user_id)}];
    
    @weakify(self);
    [self.request startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSArray<NYGradeModel *> * _Nonnull data) {
        
        @strongify(self);
        self.gradeController.itemMutaArray = data;
        self.itemController.itemMutaArray = [NSMutableArray arrayWithArray:[data firstObject].know];
        [self setSuccess];
    } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
        
        [self setError:error];
    }];
}
#pragma mark - Target Methods

- (void)checkTheMessageEvent {
    
    //在线答疑
    @weakify(self);
    [NYUserManager pushOrPresentLoginIfNeed:YES inViewController:nil completion:nil dismiss:^{
        
        @strongify(self);
        if (NYUserManager.isLogin) {
            [self.navigationController pushViewController:[NYAirlineViewController new] animated:true];
        }
    }];
}

#pragma mark - Public Methods

#pragma mark - Private Method
- (void)configNavigationBar:(BaseNavigationBar *)navigationBar {
    
    [super configNavigationBar:navigationBar];
    //left
    if (!self.isShowBack) {
        self.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:[UIView new]],[[UIBarButtonItem alloc] initWithCustomView:self.classImageview]];
    }
    
    self.navigationItem.titleView = [self.controllerView configNavgationSearchView];
    //right
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:[UIView new]],[QMUINavigationButton barButtonItemWithImage:[UIImage zi_xun] position:QMUINavigationButtonPositionLeft target:self action:@selector(checkTheMessageEvent)]];
}
+ (NYViewControllerNavigationBarType)navigationBarType {
        
        return NYViewControllerNavigationBarTypeBlue;
}

#pragma mark - External Delegate

#pragma mark - UITableViewDelegate,UITableViewDataSource

#pragma mark - Setter Getter Methods
- (UIImageView *)classImageview {
    if (!_classImageview) {
        _classImageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 33)];
        [_classImageview setImage:[UIImage zu_2]];
    }
    return _classImageview;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
